//
//  ICTitleView.m
//  ICome
//
//  Created by ENN on 16/5/12.
//  Copyright © 2016年 iCom. All rights reserved.
//

#import "ICTitleView.h"
#import "ICHeaderFile.h"

@interface ICTitleView ()<UITextFieldDelegate>
@end

@implementation ICTitleView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        self.searchBar = [ICSearchBar searchBar];
        self.searchBar.delegate = self;
        [self addSubview:self.searchBar];
        [self.searchBar becomeFirstResponder];
        
        [self.searchBar addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
        
        UIButton *cancelBtn    = [UIButton buttonWithType:UIButtonTypeCustom];
        [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        [cancelBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];//SEARCH_HEIGHT_COLOR
        cancelBtn.titleLabel.font  = [UIFont systemFontOfSize:14];
        [self addSubview:cancelBtn];
        
        [cancelBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.mas_right).offset(-14);
            make.height.mas_equalTo(25);
            make.width.mas_equalTo(30);
            make.bottom.equalTo(self.mas_bottom).offset(-10);
        }];
        [cancelBtn addTarget:self action:@selector(cancel) forControlEvents:UIControlEventTouchUpInside];
        
        [self.searchBar mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.mas_left).offset(15);
            make.right.equalTo(cancelBtn.mas_left).offset(-15);
            make.height.mas_equalTo(30);
            make.bottom.equalTo(self.mas_bottom).offset(-8);
        }];
        
        
    }
    return self;
}

- (void)cancel
{
    if ([_delegate respondsToSelector:@selector(cancelBtnClicked)]) {
        [_delegate cancelBtnClicked];
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField.text.length > 0) {
        if ([_delegate respondsToSelector:@selector(searchText:)]) {
            [_delegate searchText:textField.text];
        }
        [textField endEditing:YES];
    }
    return YES;
}
- (void)textFieldDidChange:(UITextField *)sender {
    if ([_delegate respondsToSelector:@selector(searchText:)]) {
        [_delegate searchText:sender.text];
    }
}
@end
